import * as SettingApi from '@src/api/SettingApi';
import * as ImApi from '@src/api/ImApi';

export default {
  namespaced: 'service',
  state: {
    initData: {},
    serviceConfig: {},
    dispatchRuleList: [],
    selectUsers: [],
    messageConfig: {},
    eventRemarkList: [],
    tenantImSetting: {},
  },
  mutations: {
    setInitData(state, data){
      state.initData = data;
    },
    updateServiceConfig(state, config = {}) {
      state.serviceConfig = config;
    },
    updateDispatchRuleList(state, list = []) {
      state.dispatchRuleList = list.map(
        (item, index) => (item.level = index + 1 && item)
      );
    },
    updateAllotSubList(state, list = []) {
      this.selectUsers = list.map(item => {
        item.label = item.displayName;
        item.value = item.userId;
        return item;
      });
    },
    updateMessageConfig(state, config = {}) {
      state.messageConfig = config;
    },
    updateEventRemarkList(state, list = []) {
      state.eventRemarkList = list.forEach(
        (item, index) => (item.sequence = index + 1)
      );
    },

    updateTenantImSetting(state, config = {}) {
      state.tenantImSetting = config;
    },
  },
  actions: {
    /**
     * 获取服务配置
     */
    async getServiceStationConfig({ commit }) {
      const res = await SettingApi.getServiceStationConfig();
      if (!res.succ)
        return Promise.reject('getServiceStationConfig error', res);
      commit('updateServiceConfig', res.data);
    },
    /**
     *
     */
    async getEventDispatchRules({ commit }) {
      const res = await SettingApi.getEventDispatchRules();
      if (!res.succ) return Promise.reject('getEventDispatchRules error', res);
      commit('updateDispatchRuleList', res.data);
    },

    async getAllotSubList({ commit }) {
      const res = await SettingApi.getAllotSubList();
      if (!res.succ) {
        return Promise.reject('getAllotSubList error', res);
      }
      commit('updateAllotSubList', res.data);
    },

    async getMessageConfig({ commit }) {
      const res = await SettingApi.getMessageConfig();
      if (!res.succ) {
        return Promise.reject('getMessageConfig error', res);
      }
      commit('updateMessageConfig', res.data);
    },

    async getEventRemarkList({ commit }) {
      const res = await SettingApi.getEventRemarkList();
      if (!res.succ) {
        return Promise.reject('getEventRemarkList error', res);
      }
      commit('updateEventRemarkList', res.data);
    },

    async saveEventRemarkState({ commit, dispatch }, { key, state }) {
      const params = {
        message: key,
        state,
      };
      try {
        let res = await SettingApi.saveEventRemarkState(params);
        if (!res.succ) return Promise.reject('saveEventRemarkState error', res);
      } finally {
        dispatch('getMessageConfig');
      }
    },

    async saveSwitchState({ commit, dispatch }, { key, state }) {
      const params = {
        flow: key,
        state,
      };
      try {
        let res = await SettingApi.saveSwitchState(params);
        if (!res.succ) return Promise.reject('saveSwitchState error', res);
      } finally {
        dispatch('getServiceStationConfig');
      }
    },

    async saveRuleState({ commit, dispatch }, params) {
      try {
        let res = await SettingApi.saveRuleState(params);
        if (!res.succ) return Promise.reject('saveRuleState', res);
      } finally {
        dispatch('getEventDispatchRules');
      }
    },

    /**
     * 获取 IM config
     */
    async getTenantImSetting({ commit }) {
      const res = await ImApi.getTenantImSetting();
      if (!res.succ) return Promise.reject('getTenantImSetting', res);
      commit('updateTenantImSetting', res.data);
    },

    /**
     * 设置 IM config
     */
    async ModifyImSetting({ dispatch }, params) {
      try {
        await ImApi.modifyImSetting(params);
      } finally {
        dispatch('getTenantImSetting');
      }
    },
  },
};
